﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlayerDetailsForm.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the PlayerDetailsForm type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC
{
    #region Namespace Imports

    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;

    using DataModel;

    #endregion


    /// <summary>
    /// Describes and manipulates the personal details form.
    /// </summary>
    public partial class PlayerDetailsForm : Form
    {
        #region Constants and Fields

        private readonly ApplicationData _applicationData;
        private readonly Player _player;

        #endregion


        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PlayerDetailsForm"/> class.
        /// </summary>
        /// <param name="player">
        /// The player.
        /// </param>
        /// <param name="applicationData">
        /// The application data.
        /// </param>
        public PlayerDetailsForm(Player player, ApplicationData applicationData)
        {
            _player = player;
            _applicationData = applicationData;

            InitializeComponent();

            DialogResult = DialogResult.OK;
            _nameTextBox.Text = player.Name;
        }

        #endregion


        #region Properties

        /// <summary>
        /// Gets the player.
        /// </summary>
        /// <value>
        /// The player.
        /// </value>
        public Player Player
        {
            get
            {
                return _player;
            }
        }

        #endregion


        #region Methods

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.Closing"/> event.
        /// </summary>
        /// <param name="e">
        /// A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.
        /// </param>
        protected override void OnClosing(CancelEventArgs e)
        {
            if (DialogResult != DialogResult.OK)
            {
                return;
            }

            string name = _nameTextBox.GetText();

            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show(
                    "Please specify the player name.", 
                    "Error", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Hand, 
                    MessageBoxDefaultButton.Button1);

                e.Cancel = true;

                return;
            }

            if (_applicationData.Players.Any(p => p.Name == name))
            {
                MessageBox.Show(
                    "The player with the same name already exists.", 
                    "Error", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Hand, 
                    MessageBoxDefaultButton.Button1);

                e.Cancel = true;

                return;
            }

            _player.Name = name;
        }


        private void CancelMenuItemClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        #endregion
    }
}